
$(function () {
    var scoreReg = /^[0-9]+[0-9]*$/;
    $("body").on('blur','input.input-score-box',function () {//监听分数输入
        var _this = $(this);
        var id = _this.data('id');
        var type = _this.data('type');
        var index = _this.data('index');
        var score = 0;
        if(!scoreReg.test(_this.val())){
            _this.val('');
        }else{
            score = _this.val();
        }
        var root = $(_this.parents('div.well')[0]);
        var comment = root.find('textarea.comment-box').val();
        updateQuestionRecordScore(id,type,index,score,comment);
    }).on('blur','textarea.comment-box',function () {//监听评语
        var _this = $(this);
        var id = _this.data('id');
        var type = _this.data('type');
        var index = _this.data('index');
        var root = $(_this.parents('div.well')[0]);
        var score = root.find('input.input-score-box').val();
        if(!scoreReg.test(score)){
            root.find('input.input-score-box').val('');
            score = 0;
        }
        var comment = _this.val();
        updateQuestionRecordScore(id,type,index,score,comment);
    }).on('click','#saveMarkReturn',function () {//保存返回
        var index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(index);
        // window.location.href = baseloaction +'/admin/exam/record/findExamRecordList';
    }).on('click','#saveMarkFinish',function () {//保存发布
        var examRecordId = $(this).data('id');
        icModale(2,'保存发布后，试卷将不可再修改试卷分数及添加评语，确认要发布？',3,function () {
            finishMarkExam(examRecordId);
        });
    });

    //加载答题卡
    var examRecordId = $("#examRecordId").val();
    var userId = $("#userId").val();
    loadCard(userId,examRecordId);

    //加载试题列表
    var status = $("#status").val();
    var marking = $("#marking").val();
    var marked = $("#marked").val();
    var type = $("#type").val();
    loadQuestionList({
        'userId':userId,
        'examRecordId':examRecordId,
        'status':status,
        'marking':marking,
        'marked':marked,
        'type':type,
        'pageNo':1
    });

    //监听页面滚动
    listenerScroll();

    /**
     * 完成批阅
     * @param examRecordId 考试记录ID
     */
    function finishMarkExam(examRecordId) {
        $.ajax({
            url:baseloaction +'/admin/exam/record/finishMarkExam',
            type:'post',
            dataType:'json',
            data:{
                'examRecordId':examRecordId
            },
            success:function (result) {
                if(result.success==false){
                    icModale(0,result.message);
                }else{

                    parent.layer.open({
                        type: 2,
                        area: winArea,
                        btn: ['关闭'],
                        content: baseloaction + "/admin/classes/task/userTaskMarking?examRecordId="+examRecordId,
                        maxmin: true,
                        shade: [0.3, '#000'],
                        shadeClose: false,
                        title: '作业批阅'
                    });
                    var index = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(index);
                }
            },
            error:function (error) {
                icModale(0,'系统错误，保存发布失败');
            }
        });
    }

    /**
     * 修改试题分数
     * @param id 试题记录ID
     * @param type 类型，1无子试题，2有子试题
     * @param index 子试题下标
     * @param score 分数
     * @param comment 评语
     */
    function updateQuestionRecordScore(id,type,index,score,comment){
        var examRecordId = $("#examRecordId").val();
        var userId = $("#userId").val();
        $.ajax({
            url:baseloaction +'/admin/exam/record/updateQuestionRecordScore',
            type:'post',
            dataType:'json',
            data:{
                'examRecordId':examRecordId,
                'id':id,
                'type':type,
                'index':index,
                'score':score,
                'comment':comment,
                'userId':userId
            },
            success:function (result) {
                if(!result.success){
                    icModale(0,result.message);
                }
            },
            error:function (error) {
                icModale(0,'系统错误，保存试题评分或评语失败');
            }
        });
    }
});